home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v10n11.arc / WINMEM.C < prev    next >
C/C++ Source or Header  |  1991-05-10  |  3KB  |  93 lines

  1. /* 
  2. WINMEM.C -- How much memory can we allocate under Windows?
  3.  
  4. cl -c -AS -Gsw -Oais -Zpe winmem.c
  5. link /align:16 winmem,,,/nod:slibcew libw,win.def
  6. rc winmem.exe
  7.  
  8. ; WIN.DEF -- generic Windows .DEF file
  9. EXETYPE     WINDOWS
  10. STUB        'WINSTUB.EXE'
  11. CODE        PRELOAD MOVEABLE DISCARDABLE
  12. DATA        PRELOAD MOVEABLE MULTIPLE
  13. HEAPSIZE    10240
  14. STACKSIZE   5120
  15.  
  16. or (Borland C++ 2.0; no .DEF file required):
  17.     bcc -W winmem.c
  18.     rc winmem.exe
  19.  
  20. Copyright (c) 1991 Ziff Communications Co.
  21.     PC Magazine * Andrew Schulman
  22. */
  23.  
  24. #include <stdlib.h>
  25. #include <stdarg.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <time.h>
  29. #include <dos.h>
  30. #include "windows.h"
  31.  
  32. /* printf to a Windows MessageBox */
  33. int msgprintf(char *fmt, ...)
  34. {
  35.     static char s[1024];
  36.     int len;
  37.     va_list marker;
  38.     va_start(marker, fmt);
  39.     len = wvsprintf((char far *) s, (char far *) fmt, marker);
  40.     va_end(marker);
  41.     MessageBox(NULL, s, "PC Magazine Windows Memory Allocation Test", MB_OK);
  42.     return len;
  43. }
  44.  
  45. /* return name of current Windows mode - far ptr for use with wvsprintf */
  46. char far *win_mode(void)
  47. {
  48.     unsigned long flags = GetWinFlags();
  49.     return (flags & WF_ENHANCED ? "Enhanced" :
  50.     flags & WF_STANDARD ?         "Standard" :
  51.     /* default */                 "Real") ;
  52. }
  53.  
  54. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  55.     LPSTR lpszCmdLine, int nCmdShow)
  56. {
  57.     unsigned long blksize = 32L << 10L;     /* 32k per block */
  58.     unsigned long freespace, largest, allocated, save;
  59.     time_t t1, t2;
  60.     char far *fp;
  61.  
  62.     /* retrieve these BEFORE we start allocating memory */
  63.     freespace = GetFreeSpace(0);
  64.     largest = GlobalCompact(-1);
  65.  
  66.     /* allocate as much memory as possible */
  67.     time(&t1);
  68.     allocated = 0;
  69.     while (fp = GlobalLock(GlobalAlloc(GMEM_MOVEABLE, blksize)))
  70.     {
  71.         *fp = 'x';
  72.         fp[blksize-1] = 'y';
  73.         allocated += blksize;
  74.         save = fp;
  75.         Yield();    /* Let another program run */
  76.     }
  77.     time(&t2);
  78.  
  79.     /* display results */
  80.     msgprintf("Running in %s mode\n"
  81.               "%lu bytes available\n"
  82.               "%lu bytes in largest block\n"
  83.               "Allocated %lu bytes in %lu seconds\n"
  84.               "Sample pointer: %04X:%04X", 
  85.               win_mode(),
  86.               freespace, largest, allocated, t2 - t1,
  87.               FP_SEG(save), FP_OFF(save));
  88.     
  89.     /* allocated memory will be freed automatically on exit */
  90.     return 0;
  91. }
  92.  
  93.